classdef GR_TickValue
    properties
        Num
        RoundUnit
        Unit
        MinUnit
        Mid
        Symmetric
    end
    methods
        function obj=GR_TickValue(Fmt)
            arguments
                Fmt.Num double      =   4;
                Fmt.RoundUnit double=   0.5;
                Fmt.Unit double     =   [];
                Fmt.MinUnit double  =   0;
                Fmt.Mid double=   [];
                Fmt.Symmetric int64 =   0;
            end
            obj.Num         =   Fmt.Num;
            obj.Unit        =   Fmt.Unit;
            obj.RoundUnit   =   Fmt.RoundUnit;
            obj.MinUnit     =   Fmt.MinUnit;
            obj.Mid         =   Fmt.Mid;
            obj.Symmetric   =  Fmt.Symmetric;
        end
        function YTicksVec=Setup(obj,Data)
            YMin  = min(Data(:)); YMax = max(Data(:));
            if obj.Symmetric==1
                if isempty(obj.Mid); YMid = 0; else; YMid = obj.Mid; end
                MaxGap          =   max(abs(YMin-YMid),abs(YMax-YMid));
                YMin            =   YMid-MaxGap; 
                YMax            =   YMid+MaxGap;
                if isempty(obj.Unit)
                    YGap            =   max(abs(YMax-YMin),1e-5);
                    YExpNum         =   floor(log(YGap)/log(10));
                    YInterval       =   ceil(YGap/(10^YExpNum)/obj.Num/obj.RoundUnit)*...
                                        obj.RoundUnit*(10^YExpNum);
                    YInterval       =   max(obj.MinUnit,YInterval);
                else
                    YInterval       =   obj.Unit;
                end
                Y1              =   YMid+ceil(MaxGap/YInterval)*YInterval;
                Y0              =   YMid*2-Y1;
                YTicksVec       =   (Y0:YInterval:Y1)';
            else
                if isempty(obj.Unit)
                    YGap            =   max(abs(YMax-YMin),1e-5);
                    YExpNum         =   floor(log(YGap)/log(10));
                    YInterval       =   ceil(YGap/(10^YExpNum)/obj.Num/obj.RoundUnit)*...
                                        obj.RoundUnit*(10^YExpNum);
                    YInterval       =   max(obj.MinUnit,YInterval);
                else
                    YInterval       =   obj.Unit;
                end
                Y1              =   max(ceil(YMax/YInterval),1)*YInterval;
                Y0              =   floor(YMin/YInterval)*YInterval;
                YTicksVec       =   (Y0:YInterval:Y1)';
            end
        end
    end
end


